package com.caine.androidtask.ls01;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.caine.androidtask.R;

public class DrawView extends View{
	private float current_X = 10.0f;
	private float current_Y = 10.0f;
	Bitmap bitmap = null;	
	private int speed = 10;

	public DrawView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.plane);
	}

	public DrawView(Context context,AttributeSet attr) {
		super(context,attr);	
		bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.plane);
	}
	

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		Paint paint = new Paint();
		canvas.drawBitmap(bitmap, current_X, current_Y, paint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		this.current_X = event.getX();
		this.current_Y = event.getY();
		this.invalidate();
		return true;
	}
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		System.out.println("event.getKeyCode()"+event.getKeyCode());
		switch(event.getKeyCode()){		
		case KeyEvent.KEYCODE_DPAD_UP:
			this.current_Y += speed;
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			this.current_Y -= speed;
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			this.current_X -= speed;
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			this.current_X += speed;
			break;
		}
		this.invalidate();
		return true;
	}
	

}
